#ifndef _CHANNEL_H_
#define _CHANNEL_H_


#define CHANNEL_INVALID					-1
#define CHANNEL_DOWN					0
#define CHANNEL_RESERVED				1
#define CHANNEL_OFFHOOK					2
#define CHANNEL_DIALING					3
#define CHANNEL_RING					4
#define CHANNEL_RINGING					5
#define CHANNEL_UP					6
#define CHANNEL_BUSY					7
#define CHANNEL_PRERING					8
#define CHANNEL_UNUSE					100

#define CAUSE_NORMAL_CLEARING				0
#define CAUSE_CALL_REJECTED				1
#define CAUSE_UNKNOWN					2

#define HANGUP_CAUSE_UNKNOWN				0
#define HANGUP_CAUSE_NORMAL_CLEARING			16
#define HANGUP_CAUSE_USER_BUSY				17


typedef struct {
	unsigned int inited: 1;
	unsigned int unparked: 1;

	ch_t ch;
	call_t call;

	char original_unique_id[32];
	char atxfer_from[32];

	int state;
	int hangup_cause;

	time_t t_new;
	time_t t_state;
	time_t t_hangup;

} channel_t;


extern char *_channel_proto(int proto);
extern char *_channel_type(int type);

extern int channel_new(char *channel, char *unique_id, char *caller_id, char *exten, int state);
extern int channel_newstate(char *channel, int state, char *caller_id);
extern int channel_dial_begin(char *channel, char *caller_id, char *dial_string, char *destination, char *dest_unique_id);
extern int channel_dial_end(char *channel, char *dial_status);
extern int channel_link(char *channel1, char *channel2);
extern int channel_unlink(char *channel1, char *channel2);
extern int channel_hangup(char *channel, int cause, char *caller_id);
extern int channel_masquerade(char *clone, char *original);
extern int channel_newname(char *channel, char *newname, char *unique_id);
extern int channel_queue_join(char *channel, char *caller_id, char *queue);
extern int channel_queue_leave(char *channel, char *queue);
extern int channel_moh_start(char *channel);
extern int channel_moh_stop(char *channel);
extern int channel_dtmf(char *channel, char *digit, int is_received, int is_begin, int is_end);
extern int channel_unparked_call(char *channel, char *from);
extern int channel_fax_sent(char *channel, char *filename);
extern int channel_fax_received(char *channel, char *filename, char *rsid);
extern int channel_meetme_join(char *channel, char *meetme, char *usernum);
extern int channel_meetme_leave(char *channel, char *meetme, char *usernum);
extern int channel_meetme_kick(char *meetme, char *usernum, char *errmsg);
extern int channel_redirect(char *channel);
extern int channel_atxfer(char *channel, char *from);
extern int channel_agent_login(char *channel, char *agent);
extern int channel_dump(char *prefix);

extern int channel_set_call_id(char *unique_id, char *caller_id, char *called_id, char *errmsg);
extern int channel_set_flag(char *unique_id, int flag);
extern int channel_set_flag_by_channel(char *channel, int flag);

extern int channel_get_caller_id(char *unique_id, char *caller_id, char *errmsg);
extern int channel_get_ch(char *unique_id, char *channel, char *chan, char *ch, char *proto, char *type);
extern int channel_get_state(char *channel);
extern int channel_get_meeting_id(char *channel, char *meeting_id);

extern int channel_geti(char *chan, int id, channel_t *oc);
extern int channel_get(char *unique_id, channel_t *oc);


#endif
